package com.jacobson.huobi.components.breathing

import android.animation.Animator
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.ViewGroup
import android.view.animation.LinearInterpolator
import android.widget.LinearLayout
import androidx.core.animation.addListener
import androidx.core.animation.doOnEnd
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.GridLayoutManager
import com.jacobson.core.widget.adapter.ListRecyclerAdapter
import com.jacobson.core.widget.storage.GlobalStorage
import com.jacobson.huobi.Constants
import com.jacobson.huobi.R
import com.jacobson.huobi.components.CommonListRecyclerAdapter
import com.jacobson.huobi.components.SpaceItemDecoration
import com.jacobson.huobi.components.breathing.BreathingLightGroup.ViewHolder
import com.jacobson.huobi.entities.SystemActiveInfo
import kotlinx.android.synthetic.main.item_breathing_light_view.view.*
import kotlinx.android.synthetic.main.view_breathing_light_view.view.*

class BreathingLightGroup @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {

    private val breathingLightAdapter =
        BreathingLightAdapter(context).apply {
            update(GlobalStorage.get(Constants.KEY_SYSTEM_STATUS) as List<SystemActiveInfo>)
        }
    private var valueAnimator: ValueAnimator? = null


    override fun onFinishInflate() {
        super.onFinishInflate()
        val inflate =
            LayoutInflater.from(context).inflate(R.layout.view_breathing_light_view, this, true)
        with(inflate.breathingLightRv) {
            layoutManager = GridLayoutManager(context, 3)
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(SpaceItemDecoration(1f))
            adapter = breathingLightAdapter
        }
        startAnim()
    }

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        return true
    }

    private fun startAnim() {
        stopAnim()
        valueAnimator = ValueAnimator.ofInt(255, 0, 255).apply {
            duration = 2000
            repeatCount = ValueAnimator.INFINITE
            repeatMode = ValueAnimator.RESTART
            interpolator = LinearInterpolator()
            addUpdateListener {
                breathingLightAdapter.notifyItemRangeChanged(
                    0,
                    breathingLightAdapter.getCustomItemCount(),
                    it.animatedValue
                )
            }
            addListener(object : Animator.AnimatorListener {
                override fun onAnimationRepeat(animation: Animator?) {
                }
                override fun onAnimationEnd(animation: Animator?) {
                    // 每次动画结束的时候更新一次数据
                    update(GlobalStorage.get(Constants.KEY_SYSTEM_STATUS) as List<SystemActiveInfo>)
                }
                override fun onAnimationCancel(animation: Animator?) {
                }
                override fun onAnimationStart(animation: Animator?) {
                }
            })
            start()
        }
    }

    private fun stopAnim() {
        valueAnimator?.cancel()
        valueAnimator?.end()
    }

    fun update(breathingLightInfo: List<SystemActiveInfo>) {
        breathingLightAdapter.update(breathingLightInfo)
    }

    class BreathingLightAdapter(context: Context) :
        CommonListRecyclerAdapter<SystemActiveInfo>(context) {
        override fun onCreateContentViewHolder(
            parent: ViewGroup,
            viewType: Int
        ): ContentViewHolder<SystemActiveInfo> {
            return ViewHolder(context, parent)
        }
    }

    private class ViewHolder(context: Context?, parent: ViewGroup) :
        ListRecyclerAdapter.ContentViewHolder<SystemActiveInfo>(
            context, parent, R.layout.item_breathing_light_view
        ) {
        override fun onBindView(
            items: List<SystemActiveInfo>,
            item: SystemActiveInfo,
            position: Int
        ) {
            itemView.itemBreathingLightView.color = Color.parseColor(item.color)
            itemView.itemBreathingLightView.status = item.status
            itemView.itemBreathingLightView.setBgAlpha(255)
        }

        override fun onBindView(
            items: List<SystemActiveInfo>,
            item: SystemActiveInfo,
            position: Int,
            payloads: MutableList<Any>
        ) {
            if (!payloads.isNullOrEmpty()) {
                itemView.itemBreathingLightView.setBgAlpha(payloads[0] as Int)
            }
        }
    }
}